package com.yuji.aacar.widgets.takephoto.util;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import com.yuji.aacar.widgets.takephoto.app.CustomHelper;

import java.io.File;

/**
 */
public class TFileUtils {

    private static final String TAG="TFileUtils";
    private static String DEFAULT_DISK_CACHE_ROOT = "/AaCar/";
    public static String DEFAULT_DISK_CACHE_DIR = CustomHelper.AACAR_THUMB_DIR;

    public static File getPhotoCacheDir(Context context, File file) {
//        File cacheDir = context.getCacheDir();
        File cacheDir=new File(Environment.getExternalStorageDirectory(), DEFAULT_DISK_CACHE_ROOT);
        if (!file.getParentFile().exists())file.getParentFile().mkdirs();
        if (cacheDir != null) {
            File mCacheDir = new File(cacheDir,DEFAULT_DISK_CACHE_DIR);
            if (!mCacheDir.mkdirs() && (!mCacheDir.exists() || !mCacheDir.isDirectory())) {
                return file;
            }else {
                return new File(mCacheDir, file.getName());
            }
        }
        if (Log.isLoggable(TAG, Log.ERROR)) {
            Log.e(TAG, "default disk cache dir is null");
        }
        return file;
    }

    public static void delete(String path) {
        try {
            if(path == null) {
                return ;
            }
            File file = new File(path);
            if(!file.delete()) {
                file.deleteOnExit();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
